﻿Imports System.IO.IsolatedStorage
Imports System.Windows.Forms
Imports Runa.Foundation.Diagnostics
Imports System.Collections.Concurrent

Public Class FormViewLog

    Public Property GameName() As String
    Public Property GameTracer() As Tracer

    Private Sub FormViewLog_Load(sender As Object, e As EventArgs) Handles Me.Load
        Me.GameTracer = Tracer.Add(Me.Name, Path.Combine(Me.GameName, Me.GameName))
        For Each log In Tracer.Logs.Reverse()
            Dim node = Me.TV_Log.Nodes.Add(log.Key)
            For Each item In log.Value.Log
                node.Nodes.Add(item.Item1.ToString() & " " & item.Item2)
            Next
        Next
    End Sub

    Private Sub Btn_Return_Click(sender As Object, e As System.EventArgs) Handles Btn_Return.Click
        Me.Close()
    End Sub

    Private Sub Btn_Export_Click(sender As Object, e As System.EventArgs) Handles Btn_Export.Click
        Using sfd As New SaveFileDialog()
            With sfd
                .Title = "Save log"
                .Filter = "XML file(*.xml)|All files(*.*)"
                If .ShowDialog() = Windows.Forms.DialogResult.OK Then
                    Using fs = .OpenFile()
                        Dim data = Me.GameTracer.GetData()
                        fs.Write(Data, 0, Data.Length)
                        fs.Flush()
                    End Using
                End If
            End With
        End Using
    End Sub

    Private Sub TV_Log_NodeMouseDoubleClick(sender As Object, e As TreeNodeMouseClickEventArgs) Handles TV_Log.NodeMouseDoubleClick
        MessageBox.Show(e.Node.ToString())
    End Sub

End Class